import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:mvvm_provider/page/bookshelf/view_model/bookshelf_detail_view_model.dart';
import 'package:mvvm_provider/page/bookshelf/view_model/bookshelf_offset_view_model.dart';
import 'package:mvvm_provider/page/collection/view/classic_case_page.dart';
import 'package:mvvm_provider/page/collection/view/collection_page.dart';
import 'package:mvvm_provider/page/collection/view_model/collection_view_model.dart';
import 'package:mvvm_provider/page/collection/view_model/handle_data_view_model.dart';
import 'package:mvvm_provider/page/home/view_model/home_detail_view_model.dart';
import 'package:mvvm_provider/page/home/view_model/home_view_model.dart';
import 'package:mvvm_provider/page/home/widgets/test.dart';
import 'package:mvvm_provider/page/stream_provider_test/view/stream_provider_page.dart';
import 'package:provider/provider.dart';
import '../page/bookshelf/view/bookshelf_detail_page.dart';
import '../page/future_provider_test/view/future_provider_page.dart';
import '../page/home/view/home_detail_page.dart';
import '../page/home/view/text_page.dart';
import '../page/stream_provider_test/view_model/stream_provider_view_model.dart';
import '../page/tabbar/view/tabber_page.dart';
import 'irouter_provider.dart';

class HomeRouter extends IRouterProvider {
  static const messageDetailPage = "/pages/home/message_module/InformationDetailPage"; //资讯详情
  static const homeDetailPage = "/pages/home/novel_detail/homeDetailPage"; //首页详情
  static const bookShelfDetailPage = "/pages/home/bookshelf/bookShelfDetailPage"; //书架详情

  static const biliTabarPage = "/pages/home/tabbar/BiliTabarPage"; //小说详情
  static const tabberPage = "/pages/home/tabbar/TabberPage"; //小说详情

  static const testPage = "/pages/home/tabbar/TextPage"; //小说详情

  static const waterMarkPage = "/pages/home/tabbar/WaterMarkPage"; //小说详情

  static const streamProviderPage = "/pages/stream_provider_test/StreamProviderPage";

  static const futureProviderPage = "/pages/future_provider_test/FutureProviderPage";

  static const classicCasePage = "/pages/collection/view/ClassicCasePage";

  static const collectionPage = "/pages/collection/view/CollectionPage";

  @override
  void initRouter(FluroRouter router) {
    // /// 资讯详情
    // router.define(messageDetailPage, handler: Handler(handlerFunc: (context, params) {
    //   Map? argument = context!.settings!.arguments as Map?;
    //   int entityId = argument?['entityId'] ?? 0;
    //   return const TextPage();
    // }));
    //
    // /// 资讯详情
    // router.define(testPage, handler: Handler(handlerFunc: (context, params) {
    //   return TextPage();
    // }));

    // 使用 ChangeNotifierProvider.value（直接传入已存在的对象）
    // final myNotifier = MyChangeNotifier(); // 外部创建的对象
    // ChangeNotifierProvider.value(
    //   value: myNotifier, // 不会自动释放！
    //   child: MyWidget(),
    // ),
    // 不会自动调用 dispose()，因为 Provider 不知道对象的来源。
    // 必须手动调用 myNotifier.dispose()（例如在 StatefulWidget 的 dispose() 里）。

    /// 首页详情
    router.define(homeDetailPage, handler: Handler(handlerFunc: (context, params) {
      Map? argument = context!.settings!.arguments as Map?;
      String imageUrl = argument?['imageUrl'];
      return ChangeNotifierProvider<HomeDetailViewModel>(
        create: (_) => HomeDetailViewModel(),
        builder: (context, widget) {
          return HomeDetailPage(
            imageUrl: imageUrl,
          );
        },
      );
    }));

    /// 首页实现的另外一种方式
    router.define(homeDetailPage, handler: Handler(handlerFunc: (context, params) {
      Map? argument = context!.settings!.arguments as Map?;
      String imageUrl = argument?['imageUrl'];
      return ChangeNotifierProvider<HomeDetailViewModel>(
        create: (_) => HomeDetailViewModel(),
        builder: (context, widget) {
          return HomeDetailPage(
            imageUrl: imageUrl,
          );
        },
      );
    }));

    /// 书架详情
    router.define(bookShelfDetailPage, handler: Handler(handlerFunc: (context, params) {
      Map? argument = context!.settings!.arguments as Map?;
      String imageUrl = argument?['imageUrl'];
      return MultiProvider(
        providers: [
          ChangeNotifierProvider<BookShelfDetailViewModel>(
              create: (_) => BookShelfDetailViewModel()),
          ChangeNotifierProvider<BookShelfOffsetlViewModel>(
              create: (_) => BookShelfOffsetlViewModel()),
        ],
        child: BookShelfDetailPage(
          imageUrl: imageUrl,
        ),
      );
    }));

    router.define(homeDetailPage, handler: Handler(handlerFunc: (context, params) {
      Map? argument = context!.settings!.arguments as Map?;
      String imageUrl = argument?['imageUrl'];
      return ChangeNotifierProvider<HomeDetailViewModel>(
        create: (_) => HomeDetailViewModel(),
        builder: (context, widget) {
          return HomeDetailPage(
            imageUrl: imageUrl,
          );
        },
      );
    }));

    router.define(classicCasePage, handler: Handler(handlerFunc: (context, params) {
      return const ClassicCasePage();
    }));

    router.define(collectionPage, handler: Handler(handlerFunc: (context, params) {
      return MultiProvider(
        providers: [
          ChangeNotifierProvider<HandleDataViewModel>(
            create: (_) => HandleDataViewModel()..updateData(),
          ),
          ProxyProvider<HandleDataViewModel, CollectionViewModel>(
            update: (_, handleDataViewModel, previous) => CollectionViewModel(handleDataViewModel),
          ),
        ],
        child: const CollectionPage(),
      );
    }));

    router.define(streamProviderPage, handler: Handler(handlerFunc: (context, params) {
      return StreamProvider<String>(
        initialData: '暂无数据',
        create: (context) => StreamProviderViewModel().getChatMessages(),
        builder: (context, widget) {
          return const StreamProviderPage();
        },
      );
    }));

    router.define(streamProviderPage, handler: Handler(handlerFunc: (context, params) {
      return StreamProvider<String>(
        initialData: '暂无数据',
        create: (context) => StreamProviderViewModel().getChatMessages(),
        builder: (context, widget) {
          return const StreamProviderPage();
        },
      );
    }));

    // router.define(futureProviderPage, handler: Handler(handlerFunc: (context, params) {
    //   return FutureProvider<String>(
    //     initialData: '暂无数据',
    //     create: (context) => ,
    //     builder: (context, widget) {
    //       return const FutureProviderPage();
    //     },
    //   );
    // }));
  }
}
